package com.bristua.barcode.customer.biz;

import com.bristua.barcode.common.exception.BizException;
import com.bristua.barcode.common.util.DateUtils;
import com.bristua.barcode.common.util.MD5Util;
import com.bristua.barcode.common.util.SequenceUtil;
import com.bristua.barcode.customer.dao.TCustomerAccountMapper;
import com.bristua.barcode.customer.dao.TCustomerBankMapper;
import com.bristua.barcode.customer.dao.TCustomerTakeAmtMapper;
import com.bristua.barcode.customer.enums.CustomerCode;
import com.bristua.barcode.customer.model.TCustomerAccount;
import com.bristua.barcode.customer.model.TCustomerBank;
import com.bristua.barcode.customer.model.TCustomerTakeAmt;
import com.bristua.barcode.customer.model.condition.TCustomerAccountExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/8/26.
 */
@Service
@Transactional
public class ApplyCashTakeBiz {

    @Autowired
    private GetCustomerIdBiz getCustomerIdBiz;

    @Resource
    private TCustomerAccountMapper tCustomerAccountMapper;

    @Resource
    private TCustomerTakeAmtMapper tCustomerTakeAmtMapper;

    @Resource
    private TCustomerBankMapper tCustomerBankMapper;

    
    public void applyCashTake(String takeCash,String bankId,String pwd){
        if(!StringUtils.isEmpty(takeCash)){
            BigDecimal bigDecimal = new BigDecimal(takeCash);
            int i = bigDecimal.compareTo(new BigDecimal(100));
            if(i>0){
                String customerId = getCustomerIdBiz.getCustomerId();
                TCustomerAccountExample tCustomerAccountExample = new TCustomerAccountExample();
                tCustomerAccountExample.createCriteria().andCustomerIdEqualTo(customerId);
                List<TCustomerAccount> tCustomerAccounts = tCustomerAccountMapper.selectByExample(tCustomerAccountExample);
                if(tCustomerAccounts!=null && tCustomerAccounts.size()>0){
                    TCustomerAccount tCustomerAccount = tCustomerAccounts.get(0);
                    String s = MD5Util.Bit32(pwd);
                    if(!s.equals(tCustomerAccount.getAccountPwd())){
                        throw new BizException(CustomerCode.TAKE_CASH_PWD.getCode(),CustomerCode.TAKE_CASH_PWD.getMsg());
                    }
                    BigDecimal multiply = new BigDecimal(tCustomerAccount.getAccountAmt()).divide(new BigDecimal(100)).multiply(new BigDecimal(tCustomerAccount.getAccountRate()));
                    if(bigDecimal.compareTo(multiply)>0){
                        TCustomerBank tCustomerBank = tCustomerBankMapper.selectByPrimaryKey(bankId);
                        TCustomerTakeAmt tCustomerTakeAmt = new TCustomerTakeAmt();
                        tCustomerTakeAmt.setBankId(bankId);
                        tCustomerTakeAmt.setBankName(tCustomerBank.getBankName());
                        tCustomerTakeAmt.setBankNo(tCustomerBank.getBankNo());
                        tCustomerTakeAmt.setCreateTime(DateUtils.getShowDateTime());
                        tCustomerTakeAmt.setCustomerId(customerId);
                        tCustomerTakeAmt.setRealName(tCustomerBank.getRealName());
                        tCustomerTakeAmt.setStatus(0);
                        tCustomerTakeAmt.setTakeAmt(new BigDecimal(takeCash).multiply(new BigDecimal(100)).longValue());
                        tCustomerTakeAmt.setTakeAmtId(SequenceUtil.nextId());
                        tCustomerTakeAmtMapper.insert(tCustomerTakeAmt);
                    }else{
                        throw new BizException(CustomerCode.CASH_IS_NOT_ENOUGH.getCode(),CustomerCode.CASH_IS_NOT_ENOUGH.getMsg());
                    }


                }
            }else{
                throw new BizException(CustomerCode.CASH_ONE_HUNDRED.getCode(),CustomerCode.CASH_ONE_HUNDRED.getMsg());
            }
        }
    }
}
